package com.commerce.weather.Sevice;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.Calendar;

public class MyService extends Service {
    private static final String TAG = "MyService";
    private static final int INTERVAL = 1000 * 60 * 60 * 24;// 24h
    private AlarmManager manager;
    private PendingIntent pi;

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: "+"2333333333");
        manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //此处设置开启AlarmReceiver这个Service
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 8);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        Intent i = new Intent(this,AlarmReceiver.class);
        pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(),INTERVAL, pi);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        manager.cancel(pi);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
